import random
import xlrd
import xlwt


class Student:
    def __init__(self, name, first, second, third):
        # 该同学姓名（目前无重名）
        self.name = name
        # 该同学意愿食物列表
        self.will = []
        self.will.append(first)
        self.will.append(second)
        self.will.append(third)


class Foods:
    type = ["奶茶", "炒粉", "炒饭", "麻辣鸡块面", "鱼粉", "油泼面"]

    def __init__(self, food_limit):
        # 当天所拿到的餐券份数限制
        self.limit = {}
        # 最终抽签结果
        self.result = {}
        for t in range(0, 6):
            self.limit[Foods.type[t]] = food_limit[t]
            self.result[Foods.type[t]] = []

    def push_result(self, t, the_result):
        # 将结果添加保存
        self.result[t] += the_result

    def get_all_students(self):
        # 获取所有已拿到餐券的同学
        all_students = []
        for t in range(0, 6):
            all_students.extend(self.result[Foods.type[t]])
        return all_students


def category(level_will, food):
    result = []
    for stu in students:
        if stu.will[level_will] == food:
            result.append(stu)
    return result


# 保证每个人最多只拿到一份餐券
def keep_most_one():
    all_students = foods.get_all_students()
    for c_stu in c_students:
        for all_stu in all_students:
            if c_stu.name == all_stu.name:
                c_students.remove(c_stu)


# 随机抽签方案
def random_ticket(remainder):
    random_result = []
    r_seqs = random.sample(range(0, len(c_students)), remainder)
    for r_seq in r_seqs:
        random_result.append(c_students[r_seq])
    return random_result


def enough_ticket():
    enough_result = []
    for c_stu in c_students:
        enough_result.append(c_stu)
    return enough_result


# 保证只能是6层的同学参与本抽签
def floor_6():
    data = xlrd.open_workbook("student_info.xlsx")
    table = data.sheet_by_name('Sheet1')
    col_name = table.col_values(1)
    print('错误的名字：', end='')
    for stu in students:
        if stu.name not in col_name:
            print(stu.name, end=' ')
            students.remove(stu)
    print('')
    print('-----------------------------------------')

def get_students_by_excel():
    # 导入列
    data = xlrd.open_workbook("餐券意愿统计.xlsx")
    table = data.sheet_by_name('Sheet1')
    col_date = table.col_values(6)
    col_time = table.col_values(7)
    col_name = table.col_values(8)
    col_first = table.col_values(9)
    col_second = table.col_values(10)
    col_third = table.col_values(11)
    unique_name = set()
    for row in range(0, len(col_name)):
        # 判断名字是否唯一 && 是否在当前时间段
        if col_name[row] not in unique_name and col_date[row] == date and col_time[row] == time:
            unique_name.add(col_name[row])
            new_stu = Student(col_name[row], col_first[row], col_second[row], col_third[row])
            students.append(new_stu)


if __name__ == '__main__':
    # 今日份配置
    food_count = [12, 6, 6, 6, 8, 11]
    date = '4.25'
    time = '晚餐'
    # 初始化
    students = []
    foods = Foods(food_count)
    # 从提交问卷表格中获取学生
    get_students_by_excel()
    floor_6()
    # 代码主体
    for level in range(0, 3):
        for f in range(0, 6):
            # 判断该餐券种类是否已满人
            r = foods.limit.get(foods.type[f]) - len(foods.result.get(foods.type[f]))
            if r == 0:
                continue
            # 分类：按（意愿等级、所选种类）并去除已经抽中一项餐券的人
            c_students = category(level, foods.type[f])
            keep_most_one()
            # 判断是否需要抽签（若剩下的数量满足了当前需求则不用）
            if r > len(c_students):
                a_result = enough_ticket()
            else:
                a_result = random_ticket(remainder=r)
            # 将结果添加保存
            foods.push_result(foods.type[f], a_result)
    # 打印结果 并生成结果(.xls文件)
    result_book = xlwt.Workbook()
    result_sheet = result_book.add_sheet('result')
    font = xlwt.Font()
    font.bold = True
    style = xlwt.XFStyle()
    style.font = font
    for f in range(0, 6):
        food_type = foods.type[f]

        result_sheet.write(0, f, food_type, style)

        print(food_type + ':')
        for s_seq in range(0, len(foods.result[food_type])):
            name = foods.result[food_type][s_seq].name
            result_sheet.write(s_seq + 1, f, name)
            print(foods.result[food_type][s_seq].name, end=' ')
        print('')

    result_book.save('result.xls')
